package com.juqimiao.raccoon.commodity.extension;

import com.juqimiao.raccoon.commodity.appobj.CategoryGroupAO;
import com.juqimiao.raccoon.commodity.constraints.OperationNewGroup;
import com.juqimiao.raccoon.commodity.dto.SortCatgoryGroupCmd;
import com.juqimiao.raccoon.commodity.dto.query.ListCategoryGroupQry;
import com.juqimiao.linn.exception.BusinessException;
import com.juqimiao.linn.extension.Extension;
import com.juqimiao.raccoon.commodity.dto.SwitchCategoryGroupPublishCmd;
import com.juqimiao.raccoon.commodity.repository.CategoryGroupRepository;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

@Extension(Interface = CategoryGroupValidExtPt.class,
        BusinessId = "center-platform")
public class DefaultCategoryGroupValidExt implements CategoryGroupValidExtPt {

    private ValidatorFactory validatorFactory = Validation
            .byProvider(HibernateValidator.class)
            .configure()
            .failFast(true)
            .messageInterpolator(null)
            .buildValidatorFactory();

    @Autowired
    private CategoryGroupRepository repository;

    @Override
    public void validate(CategoryGroupAO categoryGroupAO, Class validationGroupclz) {
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(categoryGroupAO, validationGroupclz);
        constraintViolations.forEach(violation -> {
            throw new BusinessException(violation.getPropertyPath() + " " + violation.getMessage());
        });
    }

    @Override
    public void validate(ListCategoryGroupQry query) {
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(query, OperationNewGroup.class);
        constraintViolations.forEach(violation -> {
            throw new BusinessException(violation.getPropertyPath() + " " + violation.getMessage());
        });
    }

    @Override
    public void validate(SwitchCategoryGroupPublishCmd cmd){
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(cmd, OperationNewGroup.class);
        constraintViolations.forEach(violation -> {
            throw new BusinessException(violation.getPropertyPath() + " " + violation.getMessage());
        });
    }

    @Override
    public void validate(SortCatgoryGroupCmd cmd) {
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(cmd);
        constraintViolations.forEach(violation -> {
            throw new BusinessException(violation.getPropertyPath() + " " + violation.getMessage());
        });
    }
}
